package com.cnunicom.modules.tra.controller;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cnunicom.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cnunicom.modules.tra.entity.UnitStaffEntity;
import com.cnunicom.modules.tra.service.UnitStaffService;
import sun.management.counter.StringCounter;

import javax.validation.Valid;


/**
 * 单位职员表
 *
 * @author dzygod
 * @email dingziyuan0@gmail.com
 * @date 2020-01-16 22:05:46
 */
@RestController
@RequestMapping("tra/unitstaff")
public class UnitStaffController {
    @Autowired
    private UnitStaffService unitStaffService;
    @Autowired
    private QRCodeUtils qrCodeUtils;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("tra:unitstaff:list")
    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = unitStaffService.queryPage(params);
        List<Object> objects = unitStaffService.queryStaffMebHisPage(params);
        PageUtils page = (PageUtils)objects.get(0);
        Map<String,String> totals = (Map<String, String>) objects.get(1);
        return R.ok().put("page", page).put("list",totals);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("tra:unitstaff:info")
    public R info(@PathVariable("id") Long id){
		UnitStaffEntity unitStaff = unitStaffService.getById(id);

        return R.ok().put("unitStaff", unitStaff);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("tra:unitstaff:save")
    public R save(@RequestBody @Valid UnitStaffEntity unitStaff){
        long staffId = new SnowflakeDistributeId(0, 0).nextId();
        unitStaff.setId(staffId);
        unitStaff.setTotalMember(0);
        unitStaff.setIsDel(0);
        unitStaff.setQrCodeUrl(qrCodeUtils.packageUrlForLink(Constant.QRcodeAddress.STAFF_PROD.getValue() + staffId
                , "https://www.ycbg.vip/group1/M00/00/09/rBD0tF7g80CACrPqAA182YJn5zo816.png"
                , new String((unitStaff.getName() + "会员注册码").getBytes(StandardCharsets.UTF_8))));
        unitStaff.setCreateTime(DateUtils.getDateTime());
		unitStaffService.save(unitStaff);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("tra:unitstaff:update")
    public R update(@RequestBody UnitStaffEntity unitStaff){
		unitStaffService.updateById(unitStaff);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("tra:unitstaff:delete")
    public R delete(@RequestBody Long[] ids){
//		unitStaffService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
